const BaseController = require('./base')
const { orderServer } = require('../api')
const formidable = require('formidable')
const ftp = require('../utils/ftp')
const dayjs = require('dayjs')

class CmsController extends BaseController {
  async initData () {
    await this.app.service.cms.initData()
    return this.success()
  }

  async addPageJson () {
    const { req } = this
    const validate = this.validate({
      name: { required: true, type: 'string' }
    }, req.body)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    // 校验通过
    const result = await this.app.service.cms.addPageJson(req.body)
    return this.response(result)
  }

  async updateCmsJson () {
    const { req } = this
    const data = {
      id: req.body.id
    }
    const validate = this.validate({
      id: { required: true, type: 'number' }
    }, data)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    await this.app.service.cms.updateCmsJson(req.body)
    return this.success()
  }

  async getPageJson () {
    const { req } = this
    const { id } = req.query
    const result = await this.app.service.cms.getPageData({id, type: 'cms'})
    return this.success(result)
  }

  async getComponentCategory () {
    const result = await this.app.service.cms.getComponentCategory()
    return this.response(result)
  }

  async deleteComponent () {
    const { req } = this
    const { id } = req.body
    await this.app.service.cms.deleteComponent(id)
    return this.success()
  }

  async getH5PageJson () {
    const { req } = this
    const { id } = req.query
    const result = await this.app.service.cms.getPageData({id, type: 'h5'})
    return this.success(result)
  }

  async getPageList () {
    const { req } = this
    const data = {
      pageNum: Number(req.query.pageNum),
      pageSize: Number(req.query.pageSize),
      name: req.query.name,
      id: req.query.id,
      creator: req.query.creator,
      is_abled: req.query.isAbled,
      startDate: req.query.startDate,
      endDate: req.query.endDate
    }
    const validate = this.validate({
      pageNum: { required: true, type: 'number' },
      pageSize: { required: true, type: 'number' }
    }, data)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.getPageList(data)
    return this.success(result)
  }

  async deletePage () {
    const { req } = this
    const { id } = req.body
    const data = {
      id
    }
    const validate = this.validate({
      id: { required: true, type: 'number' }
    }, data)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.deletePage(id)
    return this.success(result)
  }
  async updatePageStatus () {
    const { req } = this
    const { id, isAbled } = req.body
    const data = {
      id,
      isAbled
    }
    const validate = this.validate({
      id: { required: true, type: 'number' },
      isAbled: { required: true, type: 'number' }
    }, data)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.updatePageStatus(id, isAbled)
    return this.success(result)
  }

  async getPageAll () {
    const result = await this.app.service.cms.getPageAll()
    return this.success(result)
  }

  async fileUpload() {
    const { req, res } = this
    const form = formidable({ multiples: true })
    form.parse(req, (err, fields, files) => {
      if (err) {
        // next(err);
        return
      }
      ftp(files.file.filepath, files.file.originalFilename).then(() => {
        return this.success(`http://118.31.246.213/${files.file.originalFilename}`)
      }
      )
    })
  }

  async login() {
    const { req } = this
    const { username, password } = req.body
    const data = {
      username,
      password
    }
    const validate = this.validate({
      username: { required: true, type: 'string' },
      password: { required: true, type: 'string' }
    }, data)

    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.login({username, password})
    if (result) {
      return this.success(result)
    } else {
      return this.error('账号密码有误')
    }
  }

  async register() {
    const { req } = this
    const { username, password } = req.body
    const data = {
      username,
      password
    }
    const validate = this.validate({
      username: { required: true, type: 'string' },
      password: { required: true, type: 'string' }
    }, data)

    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.register({username, password})
    if (result) {
      return this.success(result)
    } else {
      return this.error('账号重复，请修改账号名')
    }
  }
  async editPwd() {
    const { req } = this
    const { username, oldPwd, newPwd } = req.body
    const data = {
      username,
      oldPwd,
      newPwd
    }
    const validate = this.validate({
      username: { required: true, type: 'string' },
      oldPwd: { required: true, type: 'string' },
      newPwd: { required: true, type: 'string' }
    }, data)

    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.editPwd({username, oldPwd, newPwd})
    if (result.type === true) {
      return this.success(result)
    } else if (result.type === false) {
      return this.error(result.msg)
    }
  }
  async getLogList () {
    const { req } = this
    const data = {
      pageNum: Number(req.query.pageNum),
      pageSize: Number(req.query.pageSize),
      name: req.query.name,
      id: req.query.id,
      creator: req.query.creator,
      is_abled: req.query.isAbled,
      startDate: req.query.startDate,
      endDate: req.query.endDate
    }
    const validate = this.validate({
      pageNum: { required: true, type: 'number' },
      pageSize: { required: true, type: 'number' }
    }, data)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.getLogList(data)
    return this.success(result)
  }

  async addLog () {
    const { req } = this
    const validate = this.validate({
      name: { required: true, type: 'string' }
    }, req.body)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    // 校验通过
    const result = await this.app.service.cms.addLog(req.body)
    return this.response(result)
  }
  // 饼状图数据
  async getLogNum () {
    const result = await this.app.service.cms.getLogNum()
    return this.success(result)
  }

  async getShopList () {
    const { req } = this
    const data = {
      pageNum: Number(req.query.pageNum),
      pageSize: Number(req.query.pageSize),
      name: req.query.name,
      code: req.query.code,
      creator: req.query.creator,
      brand: req.query.brand
    }
    const validate = this.validate({
      pageNum: { required: true, type: 'number' },
      pageSize: { required: true, type: 'number' }
    }, data)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.getShopList(data)
    return this.success(result)
  }
  async addShop () {
    const { req } = this
    // const validate = this.validate({
    //   name: { required: true, type: 'string' }
    // }, req.body)
    // if (validate) {
    //   return this.error(this.getValidate(validate))
    // }
    // // 校验通过
    const result = await this.app.service.cms.addShop(req.body)
    return this.response(result)
  }
  async deleteShop () {
    const { req } = this
    const { id } = req.body
    await this.app.service.cms.deleteShop(id)
    return this.success()
  }
  async editShop () {
    const { req } = this
    await this.app.service.cms.editShop(req.body)
    return this.success()
  }
  async getPageIsable () {
    const { req } = this
    const data = {
      id: req.query.id,
    }
    console.log('data',data);
    const result = await this.app.service.cms.getPageIsable(data)
    return this.success(result)
  }

  async getUserList () {
    const { req } = this
    const data = {
      pageNum: Number(req.query.pageNum),
      pageSize: Number(req.query.pageSize),
      name: req.query.name,
      id: req.query.id,
    }
    const validate = this.validate({
      pageNum: { required: true, type: 'number' },
      pageSize: { required: true, type: 'number' }
    }, data)
    if (validate) {
      return this.error(this.getValidate(validate))
    }
    const result = await this.app.service.cms.getUserList(data)
    return this.success(result)
  }

  async deleteUser () {
    const { req } = this
    const { id } = req.body
    await this.app.service.cms.deleteUser(id)
    return this.success()
  }

  async getVisualData () {
    const { req } = this
    const data = {
      creator: req.query.creator,
    }
    const result = await this.app.service.cms.getVisualData(data)
    return this.success(result)
  }
}

module.exports = CmsController
